package com.mysiteforme.admin.controller.publish;

import com.mysiteforme.admin.service.ElectricityMessagePushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.publish.ElectricityOrder;
import com.mysiteforme.admin.service.publish.ElectricityOrderService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-18
 */
@Controller
@RequestMapping("/admin/electricityOrder")
public class ElectricityOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricityOrderController.class);

    @Autowired
    private ElectricityOrderService electricityOrderService;
    @Autowired
    private ElectricityMessagePushService electricityMessagePushService;

    @GetMapping("list")
    @SysLog("跳转订单表列表")
    public String list(){
        return "/admin/electricityOrder/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求订单表列表数据")
    public LayerData<ElectricityOrder> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricityOrder> layerData = new LayerData<>();

        map.put("page",page);
        map.put("limit",limit);

        Page<ElectricityOrder> pageData = electricityOrderService.selectOrderList(map);

        layerData.setData(pageData.getRecords());
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增订单表页面")
    public String add(){
        return "/admin/electricityOrder/add";
    }

    @PostMapping("add")
    @SysLog("保存新增订单表数据")
    @ResponseBody
    public RestResponse add(ElectricityOrder electricityOrder){
        if(StringUtils.isBlank(electricityOrder.getTradeNo())){
            return RestResponse.failure("订单编号不能为空");
        }
        if(StringUtils.isBlank(electricityOrder.getSupplyId())){
            return RestResponse.failure("供需信息id不能为空");
        }
        electricityOrderService.insert(electricityOrder);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑订单表页面")
    public String edit(Long id,Model model){
        ElectricityOrder electricityOrder = electricityOrderService.selectById(id);
        model.addAttribute("electricityOrder",electricityOrder);
        return "/admin/electricityOrder/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑订单表数据")
    public RestResponse edit(ElectricityOrder electricityOrder){
        if(null == electricityOrder.getId() || 0 == electricityOrder.getId()){
            return RestResponse.failure("ID不能为空");
        }
        if(StringUtils.isBlank(electricityOrder.getTradeNo())){
            return RestResponse.failure("订单编号不能为空");
        }
        if(StringUtils.isBlank(electricityOrder.getSupplyId())){
            return RestResponse.failure("供需信息id不能为空");
        }
        electricityOrderService.updateById(electricityOrder);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除订单表数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricityOrder electricityOrder = electricityOrderService.selectById(id);
        electricityOrder.setDelFlag(true);
        electricityOrderService.updateById(electricityOrder);
        return RestResponse.success();
    }

    @PostMapping("statistics")
    @ResponseBody
    @SysLog("首页订单统计")
    public RestResponse statistics(Model model){
        Map map = electricityOrderService.statistics();
        model.addAttribute("map",map);
        return RestResponse.success(map);
    }

    @PostMapping("updateStatus")
    @ResponseBody
    @SysLog("修改订单状态")
    public RestResponse updateStatus(@RequestParam(value = "id", required = false) Long id,
                                     @RequestParam(value = "orderStatu", required = false) String orderStatu) {
        if (null == id || 0 == id) {
            return RestResponse.failure("ID不能为空");
        }
        EntityWrapper<ElectricityOrder> orderEntityWrapper = new EntityWrapper<>();
        orderEntityWrapper.eq("id", id.toString());
        ElectricityOrder order = electricityOrderService.selectOne(orderEntityWrapper);

        order.setOrderStatu(orderStatu);
        electricityOrderService.updateById(order);
//        electricityMessagePushService.send(order);
        return RestResponse.success();
    }
}
